package org.expressme.employee.mgmt.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.expressme.employee.mgmt.Employee;
import org.expressme.employee.mgmt.Facade;

/**
 * Query employees.
 * 
 * @author Michael Liao (askxuefeng@gmail.com)
 */
public class QueryServlet extends HttpServlet {

    private Facade facade;

    public void setFacade(Facade facade) {
        this.facade = facade;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Employee> list = null;
        String name = req.getParameter("name");
        if (name==null || name.length() == 0)
            list = facade.queryAll();
        else
            list = facade.queryByName(name);
        req.setAttribute("name", name==null ? "" : name);
        req.setAttribute("result", list);
        req.getRequestDispatcher("/query.jsp").forward(req, resp);
    }

}
